// 加密：password="Aa1234"，遍历字符串每一个值转换成ascll数字，奇数加180，偶数加126，然后转换成ascll码对应的字母或字符等
// 解密： 先将获取到的ascll转换成数字形式，奇数减180，偶数减126得到password

function encryptPassword(password) {
    var encryptedPassword = "";

    for (var i = 0; i < password.length; i++) {
        var char = password.charAt(i);
        var ascii = char.charCodeAt(0);

        if (ascii % 2 === 0) {
            // 偶数
            ascii += 126;
        } else {
            // 奇数
            ascii += 180;
        }

        // 转换回字符并拼接到加密后的密码
        encryptedPassword += String.fromCharCode(ascii);
    }
    return encryptedPassword;
}

// 示例
// var password = "Aa1234";
// var encrypted = encryptPassword(password);
// console.log("Encrypted password: " + encrypted);

function decryptPassword(encryptedPassword) {
    var decryptedPassword = "";

    for (var i = 0; i < encryptedPassword.length; i++) {
        var char = encryptedPassword.charAt(i);
        var ascii = char.charCodeAt(0);

        if (ascii % 2 === 0) {
            // 偶数
            ascii -= 126;
        } else {
            // 奇数
            ascii -= 180;
        }
        // 转换回字符并拼接到解密后的密码
        decryptedPassword += String.fromCharCode(ascii);
    }
    return decryptedPassword;
}

// 示例
// var encryptedPassword = encrypted; // 使用加密后的密码
// var decrypted = decryptPassword(encryptedPassword);
// console.log("Decrypted password: " + decrypted);

// 进行账号密码自动填充相关操作
var block2 = document.getElementById("block2");
if (block2.style.display === "none") {
    // 获取存储在 localStorage 中的凭据数据
    var storedCredentialsJSON = localStorage.getItem('credentials');
    // 将获取到的 JSON 字符串解析为对象
    var storedCredentials = JSON.parse(storedCredentialsJSON);
    // 获取登录用户名和密码的输入框的值
    var username = document.getElementById("username");
    var password = document.getElementById("password");

    // 遍历存储的凭据数据，逐个进行匹配判断
    for (var key in storedCredentials) {
        if (storedCredentials.hasOwnProperty(key)) {
            var user = storedCredentials[key];
            var decrypted = decryptPassword(user.password);
            var checkbox = document.querySelector("#remenber_password input[type='checkbox']");

            if (user.status) {  // 最新登录用户，插入账号密码
                username.value = user.username;
                password.value = decrypted;
                checkbox.setAttribute('checked', true); // 设置复选框为选中状态
            } else {    // 否则根据用户输入账号匹配密码
                checkbox.setAttribute('checked', false); 
                var firsttimes = true;

                username.addEventListener("input", function (event) {
                    // 获取输入框的值
                    var inputValue = event.target.value;

                    if (firsttimes) {
                        firsttimes = false;
                    } else {
                        if (inputValue.length > 0) {    // 检测输入内容
                            if (user.remember_pwd && user.username === username.value) {
                                password.value = decrypted;
                            }
                        }
                    }
                });
            }


        }
    }
}

